package tpdisenio2009.mvp.presenter;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import tpdisenio2009.Constants;
import tpdisenio2009.Tpdisenio2009App;
import tpdisenio2009.model.beans.FantasyTeamGlobalScore;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.mvp.display.GlobalTournamentTableView;
import tpdisenio2009.services.FantasyTeamService;
import tpdisenio2009.services.TournamentDatePointsService;
import tpdisenio2009.services.TournamentDateService;

/**
 *
 * @author Nicolás Kreiff
 */
public class GlobalTournamentTablePresenter extends DefaultPresenter<GlobalTournamentTableView> {

    private User user;
    private final TournamentDatePointsService pointsService;
    private final TournamentDateService tournamentDateService;
    private final FantasyTeamService fantasyTeamService;    

    private ItemListener dateChangeListener;
    private ChangeListener pageChangeListener;

    public GlobalTournamentTablePresenter( GlobalTournamentTableView view ) {
        super( view );
        
        pointsService = new TournamentDatePointsService();
        tournamentDateService = new TournamentDateService();
        fantasyTeamService = new FantasyTeamService();
        
        
        if ( Tpdisenio2009App.loggedUser instanceof User) {
            user = (User) Tpdisenio2009App.loggedUser;
            getView().setMaxPage( pointsService.countUsers() / Constants.PAGE_SIZE + 1 );
            getView().setDates( tournamentDateService.getClosedTournamentDates() );
            TournamentDate date = getView().getDate();
            getView().setFantasyTeamName( user.getFantasyTeam().getName() );
            getView().setOwnPoints( fantasyTeamService.getFantasyTeamGlobalPointsByDate( user.getFantasyTeam(), date ) );
            getView().setOwnPosition( fantasyTeamService.getFantasyTeamGlobalPositionByDate( user.getFantasyTeam(), date ) );
            getView().setPage( 1 );
            
            //List<FantasyTeam> fantasyTeams = pointsService.getFantasyTeamsByTournamentDate( date, 0 );
            //getView().loadFantasyTeams( fantasyTeams );
            List<FantasyTeamGlobalScore> fantasyTeamScores = fantasyTeamService.getFantasyTeamsGlobalPositionByDate(date,1);
            getView().loadFantasyTeamScores( fantasyTeamScores );

            bind();
        } else {
            getView().showNotification( "<html> Su usuario es incorrecto. <br>Compruebe que no sea un Administrador o un Periodista.</html>", "Usuario Incorrecto" );
        }
    }

    public void bind() {
        dateChangeListener = new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                getView().setPage( 1 );
                TournamentDate date = getView().getDate();

                getView().setOwnPoints( fantasyTeamService.getFantasyTeamGlobalPointsByDate( user.getFantasyTeam(), date ) );
            getView().setOwnPosition( fantasyTeamService.getFantasyTeamGlobalPositionByDate( user.getFantasyTeam(), date ) );
                //List<FantasyTeam> fantasyTeams = pointsService.getFantasyTeamsByTournamentDate( date, 0 );
                //getView().loadFantasyTeams( fantasyTeams );
                List<FantasyTeamGlobalScore> fantasyTeamScores = fantasyTeamService.getFantasyTeamsGlobalPositionByDate(date,1);
                getView().loadFantasyTeamScores( fantasyTeamScores );
            }
        };
        getView().addDateChangeListener( dateChangeListener );

        pageChangeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                TournamentDate date = getView().getDate();
                int page = getView().getPage();
                //List<FantasyTeam> fantasyTeams = pointsService.getFantasyTeamsByTournamentDate( date, page-1 );
                //getView().loadFantasyTeams( fantasyTeams );
                List<FantasyTeamGlobalScore> fantasyTeamScores = fantasyTeamService.getFantasyTeamsGlobalPositionByDate(date,page);
                getView().loadFantasyTeamScores( fantasyTeamScores );
            }
        };
        getView().addPageChangeListener(pageChangeListener);
    }

    public void unbind() {
        if (dateChangeListener != null)
            getView().removeDateChangeListener( dateChangeListener );
        if (pageChangeListener != null)
            getView().removePageChangeListener( pageChangeListener );
    }

    public boolean hasDates() {
        return getView().getDate() != null;
    }

}
